<!--
 * @Descripttion: 表单表格
 * @version: 1.3
 * @Author: sakuya
 * @Date: 2023年2月9日12:32:26
 * @LastEditors: sakuya
 * @LastEditTime: 2023年2月17日10:41:47
-->

<template>
	<div class="sc-form-table" ref="scFormTable">
		<el-table :data="data" ref="table" border stripe :show-summary="showSummary" :height="height" :summary-method="remoteSummary?remoteSummaryMethod:summaryMethod" :span-method="defaultCustom?objectSpanMethod:cusObjectSpanMethod" :cell-style="cellStyle">
			<el-table-column type="index" width="60" fixed="left" v-if="!showIndex" align="center">
				<template #header>
					<el-button v-if="!hideAdd" type="primary" icon="el-icon-plus" size="small" circle @click="rowAdd"></el-button>
				</template>
				<template #default="scope">
					<div :class="['sc-form-table-handle', {'sc-form-table-handle-delete':!hideDelete}]">
						<span>{{ scope.$index + 1 }}</span>
						<el-button v-if="!hideDelete" type="danger" icon="el-icon-delete" size="small" plain circle @click="rowDel(scope.row, scope.$index)"></el-button>
					</div>
				</template>
			</el-table-column>
			<el-table-column label="" width="50" v-if="dragSort">
				<template #default>
					<div class="move" style="cursor: move;">
						<el-icon-d-caret style="width: 1em; height: 1em;"/>
					</div>
				</template>
			</el-table-column>
			<slot></slot>
			<template #empty>
				{{ placeholder }}
			</template>
		</el-table>
	</div>
</template>

<script>
import Sortable from 'sortablejs'

export default {
	props: {
		modelValue: {type: Array, default: () => []},
		addTemplate: {
			type: Object, default: () => {
			}
		},
		placeholder: {type: String, default: "暂无数据"},
		dragSort: {type: Boolean, default: false},
		hideAdd: {type: Boolean, default: false},
		hideDelete: {type: Boolean, default: false},
		showIndex: {type: Boolean, default: false},
		summary: {type: Boolean, default: false},
		showSummary: {type: Boolean, default: false},
		height: {type: [String, Number], default: "100%"},
		remoteSummary: {type: Boolean, default: false},
		summaryMethod: {type: Function, default: null},
        defaultCustom: {type: Boolean, default: false},
		cusObjectSpanMethod: {type: Function, default: null},
		cellStyle: {type: Function, default: null},
	},
	data() {
		return {
			data: []
		}
	},
	mounted() {
		this.data = this.modelValue
		if (this.dragSort) {
			this.rowDrop()
		}
	},
	watch: {
		modelValue() {
			this.data = this.modelValue
		},
		data: {
			handler() {
				this.$emit('update:modelValue', this.data);
			},
			deep: true
		}
	},
	methods: {
		rowDrop() {
			const _this = this
			const tbody = this.$refs.table.$el.querySelector('.el-table__body-wrapper tbody')
			Sortable.create(tbody, {
				handle: ".move",
				animation: 300,
				ghostClass: "ghost",
				onEnd({newIndex, oldIndex}) {
					_this.data.splice(newIndex, 0, _this.data.splice(oldIndex, 1)[0])
					const newArray = _this.data.slice(0)
					const tmpHeight = _this.$refs.scFormTable.offsetHeight
					_this.$refs.scFormTable.style.setProperty('height', tmpHeight + 'px')
					_this.data = []
					_this.$nextTick(() => {
						_this.data = newArray
						_this.$nextTick(() => {
							_this.$refs.scFormTable.style.removeProperty('height')
						})

					})
				}
			})
		},
		rowAdd() {
			const temp = JSON.parse(JSON.stringify(this.addTemplate))
			this.data.push(temp)
			this.$emit('tableAdd', this.data);
		},
		rowDel(row, index) {
			this.data.splice(index, 1)
			this.$emit('tableDel', this.data);
		},
		//插入行
		pushRow(row) {
			const temp = row || JSON.parse(JSON.stringify(this.addTemplate))
			this.data.push(temp)
		},
		//根据index删除
		deleteRow(index) {
			this.data.splice(index, 1)
		},
		//远程合计行处理
		remoteSummaryMethod(param) {
			const {columns, data} = param;
			const sums = []
			columns.forEach((column, index) => {
				if (this.totalType == 1) {
					if (index === 1) {
						sums[index] = <p>合计<br/>减掉组合后库存数量<br/>减掉库存部件后数量</p>
						return
					}
					const values = data.map(item => Number(item[column.property]));
					// console.log('values',values)
					if (!values.every(value => isNaN(value))) {
						sums[index] = values.reduce((prev, curr) => {
							// console.log('prev, curr',prev, curr)
							const value = Number(curr);
							if (!isNaN(value)) {
								return prev + curr;
							} else {
								return prev;
							}
						}, 0);
						sums[index] = sums[index].toFixed(3);
						// sums[index] += ' 元';
					} else {
						sums[index] = '';
					}
				} else {
					if (index === 1) {
						sums[index] = <p>合计</p>
						return
					}
					console.log('data', data)
					const values = data.map(item => Number(item[column.property]));
					console.log('values', values)
					if (!values.every(value => isNaN(value))) {
						sums[index] = values.reduce((prev, curr) => {
							const value = Number(curr);
							if (!isNaN(value)) {
								return prev + curr;
							} else {
								return prev;
							}
						}, 0);
						sums[index] = sums[index].toFixed(3);
						// sums[index] += ' 元';
					} else {
						sums[index] = '';
					}
				}
				// const values =  this.summary[column.property]
				// if(values){
				// 	sums[index] = values
				// }else{
				// 	sums[index] = ''
				// }
			})
			return sums
		},
        objectSpanMethod(row){
            if (row.columnIndex === 0 || row.columnIndex === 1) {
                if (row.rowIndex % 2 === 0) {
                    return {
                        rowspan: 2,
                        colspan: 1,
                    }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 0,
                    }
                }
            }
        },
	}
}
</script>
<style>
.sc-form-table .el-table thead {
  font-weight: bold;
  font-style: normal;
  text-transform: none;
  font-family: PingFang SC, PingFang SC;
  font-size: 14px; color: #333333;
}

.sc-form-table .el-table th.el-table__cell {
  background: #e6e6e6;
}

.sc-form-table .el-table.is-scrolling-none th.el-table-fixed-column--left, .el-table.is-scrolling-none th.el-table-fixed-column--right {
  background: #e6e6e6;
}
.sc-form-table .el-table tr > td {
  color: #333333 !important;
  font-style: normal;
  text-transform: none;
  font-family: PingFang SC, PingFang SC;
}
.sc-form-table .el-table td.el-table__cell div {
  line-height: 16px;
}
</style>
<style scoped>
.sc-form-table {
	width: 100%;
    border: 1px solid #c0bdbd;
}

.sc-form-table .sc-form-table-handle {
	text-align: center;
}

.sc-form-table .sc-form-table-handle span {
	display: inline-block;
}

.sc-form-table .sc-form-table-handle button {
	display: none;
}

.sc-form-table .hover-row .sc-form-table-handle-delete span {
	display: none;
}

.sc-form-table .hover-row .sc-form-table-handle-delete button {
	display: inline-block;
}

.sc-form-table .move {
	text-align: center;
	font-size: 14px;
	margin-top: 3px;
}
</style>
